#ifndef DATABASE_H
#define DATABASE_H

#include "logger.h"
#include "commonmarco.h"

#include <QObject>
#include <QTextCodec>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QTime>
#include <QSqlError>
//#include <QtDebug>
#include <QSqlDriver>
#include <QSqlRecord>

#define TABLE_REPOSITORY_INFO            "REPOSITORY_INFO"
#define TABLE_CHARACTOR_RECORD           "CHARACTOR_RECORD"

#define CREATE_TABLE_REPOSITORY \
    "create table " TABLE_REPOSITORY_INFO "(" \
    "id integer primary key,"   \
    "uri vachar(128) not null," \
    "name vachar(32)"   \
    ")"

#define CREATE_TABLE_CHARACTOR  \
    "create table " TABLE_CHARACTOR_RECORD "("\
    "id integer primary key,"   \
    "x integer not null,"   \
    "y integer not null,"   \
    "type integer not null,"    \
    "top integer not null,"  \
    "bottom integer not null,"  \
    "left integer not null,"  \
    "rigth integer not null,"  \
    "repository_id integer not null"  \
    ")"


typedef struct stRepositoryInfo
{
    int id;
    QString uri;
    QString name;
}RepositoryInfo_t;

class DataBase
{
public:
    DataBase();
    bool initDbAndTable();    //call for create xxx.db and tables,only once for the software
    bool initDriver();        //call of mainWindows construct ,only once of one exec
    bool createConnection();    //call for all options every;
//    bool createTable();     //only once
//    bool init();
    bool insertCharacotr(QString pathName,
        CharacterVector* pVecCharactor,int nSize);
    bool queryRepository(RepositoryInfo_t* pRepository,int& nSize,int maxSize);
    bool queryCharactor(int repository_id,
        CharacterVector* pVecCharactor,int& nSize,int maxSize);
    void printCount(QString tableName);

private:
    inline bool doCheck();
    inline bool getNewId(QString table,int& id);
private:
    QString m_Path;
    QString m_Name;
    QString m_dbName;
    QSqlDatabase m_db;
    QSqlQuery    m_query;

    bool        m_iIsInizFlag;
};

#endif // DATABASE_H
